<?php

namespace app\controller;
use app\BaseController;
use think\facade\Config;
use think\facade\Db;
use think\facade\Session;

class ThreeLogin extends BaseController
{

    // http://www.penyou.com/ThreeLogin/qq
    public function qq()
    {
        $config = $this->qqConfig();
        $login = \wycto\login\Login::getApp('qq',$config);
        $login->login();
    }

    /**
     * QQ登录回调 http://www.penyou.com/ThreeLogin/qqCallback
     */
    public function qqCallback()
    {
        $user =(new \app\model\Users)->getUserInfoThree('qq_openid','123');
        print_r($user);
        exit;
        $config = $this->qqConfig();
        $login = \wycto\login\Login::getApp('qq',$config);
        // 获取用户信息

        $userinfo = $login->getUserInfo();

        if (! isset($userinfo['openid']) || empty($userinfo['openid'])) {
            return $this->redirect(url("index/index/index"));
        }
        // 查询是否存在
//        $user = User::get(array(
//            'qq_openid' => $userinfo['openid']
//        ));
        $user = (new \app\model\Users)->getUserInfoThree('qq_openid',$userinfo['openid']);
        if ($user) {
            // 账号存在去登录
            return $this->_toLogin($user, false);
        } else {
            // 新注册该用户
            Session::set("qq_userinfo", $userinfo);
            return $this->redirect(url("index/user/newAccount"));
        }
    }

    private function qqConfig(){

        $threeLogin = Config::get('threeLogin');

        $config = array(
            //开发平台获取
            'app_id' => $threeLogin['qq_app_id'],
            //开发平台获取
            'app_key' => $threeLogin['qq_app_key'],
            //回掉地址，需要在腾讯开发平台填写
            'callback' => "https://my.jiuniao.com/oauth/signin/qq",
            'scope' => 'get_user_info',
            'expires_in' => 7775000
        );
        return $config;
    }

}
